import GroupBy from '../types/groupBy';
/**
 * @description 根据key将数组对象进行分类
 * @param {Array} arr 源数据
 * @param {String|Function}generatekey  分组字段/获取分组字段的回调
 * @returns {Object}result 
 */
const groupBy: GroupBy = (arr, generatekey) => {
	if (typeof generatekey === 'string') {
		const propName = generatekey;
		generatekey = (item: any) => item[propName];
	}
	const result: any = {};
	for (const item of arr) {
		const key = generatekey(item);
		if (!result[key]) {
			result[key] = [];
		}
		result[key].push(item);
	}
	return result;
};
export default groupBy;